﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Data.SqlClient;
namespace SharepointSolutionInstaller.Utils.Configuration
{
    public class InstallConfiguration
    {
        public static Guid SolutionId
        {
            get { return new Guid(ConfigurationManager.AppSettings[ConfigurationProperties.SolutionId]); }
        }
        public static Guid OfficeSolutionId
        {
            get { return new Guid(ConfigurationManager.AppSettings[ConfigurationProperties.OfficeSolutionId]); }
        }

        internal static string SolutionTitle
        {
            get { return ConfigurationManager.AppSettings[ConfigurationProperties.SolutionTitle]; }
        }
        internal static string OfficeSolutionTitle
        {
            get { return ConfigurationManager.AppSettings[ConfigurationProperties.OfficeSolutionTitle]; }
        }

        public static string SolutionFile
        {
            get { return ConfigurationManager.AppSettings[ConfigurationProperties.SolutionFile]; }
        }

        public static string OfficeSolutionFile
        {
            get { return ConfigurationManager.AppSettings[ConfigurationProperties.OfficeSolutionFile]; }
        }
        public static string FormatString(string str)
        {
            return FormatString(str, null);
        }

        public static Version InstalledVersion
        {
            get
            {
                try
                {
                    SPFarm farm = SPFarm.Local;
                    string key = "Solution_" + SolutionId.ToString() + "_Version";
                    return farm.Properties[key] as Version;
                }

                catch (NullReferenceException ex)
                {
                    throw new Exception();
                }

                catch (SqlException ex)
                {
                    throw new Exception();
                }
            }

            set
            {
                try
                {
                    SPFarm farm = SPFarm.Local;
                    string key = "Solution_" + SolutionId.ToString() + "_Version";
                    farm.Properties[key] = value;
                    farm.Update();
                }

                catch (NullReferenceException ex)
                {
                    throw new Exception();
                }

                catch (SqlException ex)
                {
                    throw new Exception();
                }
            }
        }

        public static Version SolutionVersion
        {
            get { return new Version(ConfigurationManager.AppSettings[ConfigurationProperties.SolutionVersion]); }
        }

        internal static List<Guid?> FeatureId
        {
            get
            {
                string valueStr = ConfigurationManager.AppSettings[ConfigurationProperties.FeatureId];

                //
                // Backwards compatibility with old configuration files before site collection features allowed
                //
                if (String.IsNullOrEmpty(valueStr))
                {
                    valueStr = ConfigurationManager.AppSettings[ConfigurationProperties.FarmFeatureId];
                }

                if (!String.IsNullOrEmpty(valueStr))
                {
                    string[] strGuidArray = valueStr.Split(";".ToCharArray());
                    if (strGuidArray.Length >= 0)
                    {
                        var guidArray = new List<Guid?>();
                        foreach (string strGuid in strGuidArray)
                        {
                            guidArray.Add(new Guid(strGuid));
                        }
                        return guidArray;
                    }
                }

                return null;
            }
        }

        internal static List<Guid?> OfficeFarmFeatureId {
            get
            {
                string valueStr = ConfigurationManager.AppSettings[ConfigurationProperties.OfficeFarmFeatureId];

                    string[] strGuidArray = valueStr.Split(";".ToCharArray());
                    if (strGuidArray.Length >= 0)
                    {
                        var guidArray = new List<Guid?>();
                        foreach (string strGuid in strGuidArray)
                        {
                            guidArray.Add(new Guid(strGuid));
                        }
                        return guidArray;
                    }
                    return null;
                }


            }
        
        
        
        internal static bool DefaultDeployToSRP
        {
            get
            {
                string valueStr = ConfigurationManager.AppSettings[ConfigurationProperties.DefaultDeployToSRP];
                if (!String.IsNullOrEmpty(valueStr))
                {
                    return valueStr.Equals("true", StringComparison.OrdinalIgnoreCase);
                }
                return false;
            }
        }

        internal static bool RequireDeploymentToCentralAdminWebApplication
        {
            get
            {
                string valueStr = ConfigurationManager.AppSettings[ConfigurationProperties.RequireDeploymentToCentralAdminWebApplication];

                //
                // Backwards compatability with old configuration files containing spelling error in the 
                // application setting key (Bug 990).
                //
                if (String.IsNullOrEmpty(valueStr))
                {
                    valueStr = ConfigurationManager.AppSettings[ConfigurationProperties.RequireDeploymentToCentralAdminWebApllication];
                }

                if (!String.IsNullOrEmpty(valueStr))
                {
                    return valueStr.Equals("true", StringComparison.OrdinalIgnoreCase);
                }

                return false;
            }
        }

        internal static bool RequireDeploymentToAllContentWebApplications
        {
            get
            {
                string valueStr = ConfigurationManager.AppSettings[ConfigurationProperties.RequireDeploymentToAllContentWebApplications];
                if (!String.IsNullOrEmpty(valueStr))
                {
                    return valueStr.Equals("true", StringComparison.OrdinalIgnoreCase);
                }
                return false;
            }
        }

        public static string EULA
        {
            get { return ConfigurationManager.AppSettings[ConfigurationProperties.EULA]; }
        }

        internal static string UpgradeDescription
        {
            get
            {
                string str = ConfigurationManager.AppSettings[ConfigurationProperties.UpgradeDescription];
                if (str != null)
                {
                    str = FormatString(str);
                }
                return str;
            }
        }

        internal static SPFeatureScope FeatureScope
        {
            get
            {
                // Default to farm features as this is what the installer only supported initially
                SPFeatureScope featureScope = SPFeatureScope.Site;
                string valueStr = ConfigurationManager.AppSettings[ConfigurationProperties.FeatureScope];
                if (!String.IsNullOrEmpty(valueStr))
                {
                    featureScope = (SPFeatureScope)Enum.Parse(typeof(SPFeatureScope), valueStr, true);
                }
                return featureScope;
            }
        }

        public static string FormatString(string str, params object[] args)
        {
            string formattedStr = str;
            string solutionTitle = SolutionTitle;
            if (!String.IsNullOrEmpty(solutionTitle))
            {
                formattedStr = formattedStr.Replace("{SolutionTitle}", solutionTitle);
            }
            if (args != null)
            {
                formattedStr = String.Format(formattedStr, args);
            }
            return formattedStr;
        }

        public static bool ShowFinishedControl
        {
            get
            {
                return !String.IsNullOrEmpty(ConfigurationManager.AppSettings[ConfigurationProperties.SiteCollectionRelativeConfigLink]) ||
                  !String.IsNullOrEmpty(ConfigurationManager.AppSettings[ConfigurationProperties.SSPRelativeConfigLink]);
            }
        }
    }
}
